{% load extra_filters_jinja %}

{%- macro share(site = None, site_label = None, icon = False) -%}
    <a class="{{ site }}-share{% if icon == True %} icon{% endif %}"
        title="{% trans %}Share this content on {{site}}{% endtrans %}"
    >{% if icon == False %}{% if site_label %}{{ site_label }}{% else %}{{ site }}{% endif %}{% endif %}</a>
{%- endmacro -%}

{%- macro inbox_message_snippet(message) -%}
<div class="message-details" data-message-id="{{ message.id }}">
    <input type="checkbox" />
    {{ gravatar(message.user, 16) }}
    <a class="username" href="{{ message.user.get_absolute_url() }}">{{ message.user.username|escape }}</a>
    <a class="forum-post-link" href="{{ message.url }}">{{ message.message_type }}</a>
    ({{ timeago(message.timestamp) }})
    {#<div class="snippet">{{ message.snippet }}</div>#}
    <div class="post-content">{{ message.content }}</div>
</div>
{%- endmacro -%}

{%- macro mailto_link(email) -%}
    <a href="mailto:{{ email }}">{{ email }}</a>
{%- endmacro -%}

{%- macro moderation_queue_message(message) -%}
<div class="message-details" data-message-id="{{ message.id }}">
    <input type="checkbox" />
    {{ gravatar(message.user, 16) }}
    <a class="username" href="{{ message.user.get_absolute_url() }}">{{ message.user.username|escape }}</a> |
    {% if message['memo_type'] == 'edit' %}
        {{ mailto_link(message.user.email) }} |
        ip=<span class="ip-addr">{{ message.ip_addr }}</span> |
    {% endif %}
    <a class="forum-post-link" href="{{ message.url }}">{{ message.message_type }}</a>
    ({{ timeago(message.timestamp) }})
    {#<div class="snippet">{{ message.snippet }}</div>#}
    <div class="post-content">{{ message.content }}</div>
</div>
{%- endmacro -%}

{%- macro post_vote_buttons(post = None) -%}
<div id="{{post.post_type}}-img-upvote-{{ post.id }}"
    class="{{post.post_type}}-img-upvote post-vote upvote">
</div>
<div 
    id="{{post.post_type}}-vote-number-{{ post.id }}"
    class="vote-number"
    title="{% trans %}current number of votes{% endtrans %}"
>{{ post.score }}</div>
<div
    id="{{post.post_type}}-img-downvote-{{ post.id }}"
    class="{{post.post_type}}-img-downvote post-vote downvote">
</div>
<script type="text/javascript">
    askbot['functions']['renderPostVoteButtons']('{{post.post_type}}', '{{post.id}}');
</script>
{%- endmacro -%}

{%- macro post_contributor_avatar_and_credentials(revision) -%}
    {% if revision.is_anonymous %}
        <img alt="{% trans %}anonymous user{% endtrans %}" src="{{ '/images/anon.png'|media }} " class="gravatar" width="32" height="32" />
        <p>{{ revision.author.get_anonymous_name() }}</p>
    {% else %}
        {{ user_card(revision.author) }}
    {% endif %}
    {% if settings.GROUPS_ENABLED %}
        {{ user_primary_group(revision.author) }}
    {% endif %}
{%- endmacro -%}

{%- macro post_last_updater_and_creator_info(post) -%}
    {% if post.wiki %}
    <div class='post-update-info'>
        <p>
           {%- if post.post_type == 'question' -%}
               {% set action_performed=settings.WORDS_ASKED|escape %}
           {% elif post.post_type == 'answer' %}
               {% set action_performed=settings.WORDS_ANSWERED|escape %}
           {% else %}
               {%- set action_performed=gettext('posted') %}
           {% endif %}
           <strong>{{ timeago(post.added_at, action_performed=action_performed) }}</strong>
        </p>
        <img width="35" height="35"
            src="{{'/images/wiki.png'|media}}"
            alt="{% trans %}this post is marked as community wiki{% endtrans %}"
            style="float:left"
        />
        {% set wiki_min_rep = settings.MIN_REP_TO_EDIT_WIKI %}
        <p class="tip">{% trans %}This post is a wiki.
        Anyone with karma &gt;{{wiki_min_rep}} is welcome to improve it.{% endtrans %}</p>
    </div>
    {% else %}
        {% set first_rev = post.get_earliest_revision() %}
        {% set last_rev = post.get_latest_revision() %}
        {{ post_contributor_info(first_rev, "original_author") }}
        {% if first_rev != last_rev %}
            {{ post_contributor_info(last_rev, "last_updater") }}
        {% endif %}
    {% endif %}
{%- endmacro -%}

{%- macro post_contributor_info(revision, contributor_type) -%}
<div class='post-update-info'>
{% if contributor_type=="original_author" %}
    {% if revision.post.post_type == 'question' %}
        {% set action_label = settings.WORDS_ASKED|escape %}
    {% elif revision.post.post_type == 'answer' %}
        {% set action_label = settings.WORDS_ANSWERED|escape %}
    {% else %}
        {% set action_label = gettext('posted') %}
    {% endif %}
{% else %}
    {% set action_label = gettext('updated') %}
{% endif %}
    {% if revision.post.post_type in ('question', 'answer') %}
        {% set original_author = revision.post.author %}
    {% else %}
        {% set original_author = None %}
    {% endif %}
    <p style="line-height:12px;">
        <a 
        {% if revision.post.post_type == 'question' %}
            href="{% url question_revisions revision.post.id %}"
        {% else %}
            href="{% url answer_revisions revision.post.id %}"
        {% endif %}
        ><strong>{{ timeago(revision.revised_at, action_performed=action_label) }}</strong></a>
    </p>
    {% set show_user_info = (contributor_type == 'original_author' or 
        (contributor_type == 'last_updater' and 
        original_author != revision.author ))
    %}
    {% if show_user_info %}
        {{ post_contributor_avatar_and_credentials(revision) }}
    {% endif %}
</div>
{%- endmacro -%}

{%- macro if_else(condition, if_true, if_false) -%}
    {%- if condition == True -%}
        {{if_true}}
    {%- else -%}
        {{if_false}}
    {%- endif -%}
{%- endmacro -%}

{%- macro tag_cloud(tags = None, font_sizes = None, search_state = None) -%}
    {% for tag in tags %}
    <span class="tag-size-{{ font_sizes[tag.name]|escape }}">
        <a 
            class="link-typeA"
            title="Number of entries: {{ tag.used_count }}"
            href="{{ search_state.add_tag(tag.name).full_url() }}"
        >{{ tag.name|escape }}</a>
    </span>
    {% endfor %}
{%- endmacro -%}

{%- macro tag_list_widget(
        tags,
        id=None,
        deletable=False,
        make_links=True,
        search_state=None,
        css_class=None,
        truncate_long_tags=False
    )
-%}
<ul {% if id %}id="{{ id }}"{% endif %}
    class="tags{% if css_class %} {{css_class}}{% endif %}"
>
    {% if tags %}
        {% for tag in tags %}
        <li>
            {{ tag_widget(
                tag,
                deletable=deletable,
                is_link=make_links,
                search_state=search_state,
                truncate_long_tag=False
            )}}
        </li>
        {% endfor %}
    {% endif %}
</ul>
{%- endmacro -%}

{%- macro user_group_link(group) -%}
    <a class="group-name"
        href="{% url users_by_group group.id, group.name|replace('-', ' ')|slugify %}"
    >{{ group.name|escape }}</a>
{%- endmacro -%}

{%- macro user_group(group, membership_info, show_count=False) -%}
    <td>
        {{ user_group_link(group) }}
    </td>
    {% if show_count %}
        <td>{{ group.users_count }}</td>
    {% endif %}
    <td>
    <span class="group-description">
    {% if group.description %}
        {{ group.description.summary }}
    {% endif %}
    </span>
    {% if membership_info %}
        <br/>
        {{ group_join_button(
                group_id = group.id,
                acceptance_level = membership_info['aceptance_level'],
                membership_level = membership_info['membership_level']
            )
        }}
    {% endif %}
    </td>
{%- endmacro -%}

{%- macro user_primary_group(user) -%}
    {% set group=user.get_primary_group() %}
    {% if group %}
        <span class="primary-group-name"><a
            class="primary-group-name"
            href="{% url users_by_group group.id, group.name|replace('-', ' ')|slugify %}"
        >{{ group.name|replace('-', ' ')|escape }}</a></span>
    {% endif %}
{%- endmacro -%}

{%- macro group_join_button(
    group_id=None, acceptance_level='closed', membership_level='none')
-%}
    {% if acceptance_level in ('open', 'moderated') %}
        <button 
            class="group-join-btn button"
            data-group-id="{{group_id}}"
            data-is-on="{{ membership_level|not_equal('none')|as_js_bool }}"
            {% if acceptance_level == 'open' %}
                data-off-prompt-text="{% trans %}Leave this group{% endtrans %}"
                data-on-prompt-text="{% trans %}Join this group{% endtrans %}"
                data-on-state-text="{% trans %}You are a member{% endtrans %}"
                data-off-state-text="{% trans %}Join this group{% endtrans %}"
            {% else %}
                {% if membership_level == 'full' %}
                    data-off-prompt-text="{% trans %}Leave this group{% endtrans %}"
                    data-on-state-text="{% trans %}You are a member{% endtrans %}"
                {% else %}
                    data-off-prompt-text="{% trans %}Cancel application{% endtrans %}"
                    data-on-state-text="{% trans %}Awaiting approval{% endtrans %}"
                {% endif %}
                data-on-prompt-text="{% trans %}Ask to join{% endtrans %}"
                data-off-state-text="{% trans %}Ask to join{% endtrans %}"
            {% endif %}
        >
            {% if membership_level == 'full' %}
                {% trans %}You are a member{% endtrans %}
            {% elif membership_level == 'pending' %}
                {% trans %}Awaiting approval{% endtrans %}
            {% else %}
                {% if acceptance_level == 'open' %}
                    {% trans %}Join this group{% endtrans %}
                {% else %}
                    {% trans %}Ask to join{% endtrans %}
                {% endif %}
            {% endif %}
        </button>
    {% endif %}
{%- endmacro -%}

{# todo: remove the extra content argument to make its usage more explicit #}
{%- macro tag_widget(
            tag,
            deletable=False,
            is_link=True,
            css_class=None,
            search_state=None,
            extra_content='',
            truncate_long_tag=False
        )
-%}
  {% if not search_state %}  {#  get empty SearchState() if there's none; CAUTION: for some reason this doesn't work inside `spaceless` tag below! #}
      {% set search_state=search_state|get_empty_search_state %}
  {% endif %}
  {% spaceless %}
    <div class="js-tag {% if deletable %} js-deletable-tag{% endif %}">
        <{% if is_link %}a{% else %}span{% endif %}
            class="js-tag-name{% if css_class %} {{ css_class }}{% endif %}"
            rel="tag"
            {% if is_link and not tag.endswith('*') %}
            href="{{ search_state.add_tag(tag).full_url() }}"
            {% endif %}
            data-tag-name="{{ tag|escape }}"
        >{% if truncate_long_tag -%}
                {{ tag|truncate(17, True)|escape|replace('*', '&#10045;')}}
            {%- else -%}
                {{ tag|escape|replace('*', '&#10045;') }}
            {%- endif -%}
        </{% if is_link %}a{% else %}span{% endif %}>
        {% if deletable %}
            <span class="js-delete-icon">&times;</span>
        {% endif %}
        {% if extra_content %}
        <span class="js-tag-extra-content">{{ extra_content }}</span>
        {% endif %}
    </div>
  {% endspaceless %}
{%- endmacro -%}

{%- macro radio_select(name = None, value = None, choices = None) -%}
    {% for choice in choices %}
        <p class="choice">
            {% set id = "id_" ~ name ~ "_" ~ choice[0] %}
            <input 
                id="{{ id }}"
                name="{{ name }}"
                value="{{ choice[0] }}"
                type="radio"
                {% if value == choice[0] %}
                checked="checked"
                {% endif %}
            />
            <label for="{{ id }}">{{ choice[1] }}</label>
        </p>
    {% endfor %}
{%- endmacro -%}

{%- macro question_summary(thread, question, extra_class=None, search_state=None, visitor = None) -%}
{%include "widgets/question_summary.html" %}
{%- endmacro -%}

{# Warning! Any changes to the comment markup here must be duplicated in post.js
for the purposes of the AJAX comment editor #}

{%- macro comments_widget_buttons(post = None, max_comments = None) -%}
    {% if post.comment_count > max_comments %}
        {% if settings.COMMENTS_REVERSED == False %}
            <a class="hidden js-open-editor-btn">{% trans %}add a comment{% endtrans %}</a>
        {% endif %}
        <a class="js-load-comments-btn">
            {% if settings.COMMENTS_REVERSED %}
                {% trans %}load older comments{% endtrans %}
            {% else %}
                {% trans %}see more comments{% endtrans %}
            {% endif %}
        </a>
    {% else %}
        {% if settings.COMMENTS_REVERSED == False %}
            <a class="js-open-editor-btn">{% trans %}add a comment{% endtrans %}</a>
        {% endif %}
    {% endif %}
{%- endmacro -%}

{%- macro csrf_middleware_token(csrf_token) -%}
    <div style="display: none;">
        <input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token }}" />
    </div>
{%- endmacro -%}

{%- macro comment_widget(comment, csrf_token=None) -%}
    {# Warning! Any changes to the comment markup must be duplicated in post.js
       for the purposes of the AJAX comment editor #}
    <div id="post-id-{{ comment.id }}" class="comment js-comment" data-post-id="{{ comment.id }}">
        <div class="comment-votes">
            <div 
                id="comment-img-upvote-{{ comment.id }}" 
                class="upvote js-score"
            >{% if comment.score > 0 %}{{ comment.score }}{% endif %}</div>
            {% if comment.id %}
            <script type="text/javascript">
                askbot['functions']['renderPostVoteButtons']('comment', '{{ comment.id }}');
            </script>
            {% endif %}
        </div>
        <div class="comment-content">
            <div id="post-{{ comment.id }}-delete" class="comment-delete">
                <span class="js-delete-icon" title="{% trans %}delete this comment{% endtrans %}"></span>
            </div>
            {% if comment.needs_moderation() %}
                <p class="moderated">{% trans %}This post is awaiting moderation, please do not repost the same content!{% endtrans %}</p>
            {% endif %}
            <div class="comment-body">
                {{ comment.summary }}
            </div>
            <div class="comment-controls">
                {# important - the same number for avatar size must be in question/javascript.html #}
                {{ gravatar(comment.author, 16) }}
                <a 
                    class="author"
                    href="{{ comment.author.get_profile_url() }}"
                >{{ comment.author.username|escape }}</a>
                <span class="age">&nbsp;({{ timeago(comment.added_at) }})</span>
                <a 
                    id="post-{{ comment.id }}-edit"
                    class="js-edit"
                >{% trans %}edit{% endtrans %}</a>
                <form 
                    action="{% url comment_to_answer %}"
                    method="POST"
                    accept-charset="utf-8"
                    class='convert-comment'
                >
                    <input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token }}">
                    <input type="hidden" value="{{ comment.id }}" name="comment_id">
                    <input type="submit" value="{% trans %}convert to answer{% endtrans %}">
                </form>
            </div>
        </div>
    </div>
    {% if comment.id %}
    <script type="text/javascript">
        askbot['functions']['hideConvertLinks']();
        askbot['functions']['renderPostControls']('{{ comment.id }}');
    </script>
    {% endif %}
{%- endmacro -%}

{%- macro post_comments_widget(
        post=None,
        show_post=None,
        show_comment=None,
        show_comment_position=None,
        user=None,
        max_comments=None,
        csrf_token=None
    )
-%}
    {% spaceless %}
    {% if show_post == post and show_comment and show_comment_position > max_comments %}
        {% set comments = post.get_cached_comments()[:show_comment_position] %}
    {% else %}
        {% set comments = post.get_cached_comments()[:max_comments] %}
    {% endif %}
    {% set comments_count = comments|length %}
    {% if comments_count > 0 or settings.COMMENTS_REVERSED %}
        <h2 class="comment-title">{% trans %}Comments{% endtrans %}</h2>
        <div class="clean"></div> 
    {% endif %}
    <div class="comments{% if comments_count == 0 %} empty{% endif %} ordering-{% if settings.COMMENTS_REVERSED %}reversed{% else %}forward{% endif %}" 
        data-parent-post-type="{{ post.post_type }}"
        data-parent-post-id="{{ post.id }}"
    >
        {% if settings.COMMENTS_REVERSED %}
            <div class="js-open-editor-btn new-comment-box">
                {% set prompt=gettext('add a comment...') %}
                {% include "widgets/mock_editor.html" %}
            </div>
        {% endif %}
        <div class="content">
            {% for comment in comments %}
                {{ comment_widget(comment, csrf_token) }}
            {% endfor %}
        </div>
        <div class="controls">
            {% if show_post == post and show_comment and show_comment_position > max_comments %}
                {{ comments_widget_buttons(post=post, max_comments=show_comment_position) }}
            {% else %}
                {{ comments_widget_buttons(post=post, max_comments=max_comments) }}
            {% endif %}
        </div>
    </div>
    {% endspaceless %}
{%- endmacro -%}

{%- macro reversible_sort_button(button_sort_criterium=None, asc_tooltip=None,
            desc_tooltip=None, label=None, current_sort_method=None, search_state=None) -%}
{# 
    sort button where descending sort is default
    and the search method is togglable between ascending and descending 
    buttons are rendered as links with id constructed as
    "by_" + button_sort_criterium
    class "on" is added when current_sort_method is one of
    button_sort_criterium + "asc" or "desc"
#}
    {% set key_name = button_sort_criterium %}
    {% if current_sort_method == key_name + "-asc" %}{# "worst" first #}
        <a id="by_{{key_name}}"
           href="{{ search_state.change_sort(key_name+"-desc").full_url() }}"
           class="rev on"
           title="{{desc_tooltip}}"><span>{{label}} &#9650;</span></a>
    {% elif current_sort_method == key_name + "-desc" %}{# "best first" #}
        <a id="by_{{key_name}}"
           href="{{ search_state.change_sort(key_name+"-asc").full_url() }}"
           class="rev on"
           title="{{asc_tooltip}}"><span>{{label}} &#9660;</span></a>
    {% else %}{# default, when other button is active #}
        <a id="by_{{key_name}}"
           href="{{ search_state.change_sort(key_name+"-desc").full_url() }}"
           class="off"
           title="{{desc_tooltip}}"><span>{{label}}</span></a>
    {% endif %}
    <script type="text/javascript">{# need to pass on text translations to js #}
        askbot['data']['sortButtonData'] = askbot['data']['sortButtonData'] || {};
        askbot['data']['sortButtonData']['{{key_name}}'] = {
            label: '{{label}}',
            asc_tooltip: '{{asc_tooltip}}',
            desc_tooltip: '{{desc_tooltip}}'
        };
    </script>
{%- endmacro %}

{%- macro checkbox_in_div(checkbox_field, class = 'checkbox') -%}
    <div{% if class %} class="{{class}}"{% endif %}
        title="{{checkbox_field.help_text}}">
        {{ checkbox_field.errors }}
        {{ checkbox_field }} 
        {{ checkbox_field.label_tag() }} 
    </div>
{%- endmacro -%}

{%- macro edit_post(post_form, settings, post_html=None) -%}
    {# todo: retire this macro, this will require renaming
    or using "set" on some template variables #}
    {% include "editors/rich_text.html" %}
{%- endmacro -%}

{%- macro tag_autocomplete_js(id = '#id_tags') -%}
    var tagAc = new AutoCompleter({
            url: '{% url "get_tag_list" %}',
            minChars: 1,
            useCache: true,
            matchInside: true,
            maxCacheLength: 100,
            delay: 10
    });
    tagAc.decorate($("{{ id }}"));
{%- endmacro -%}

{%- macro answer_classes(answer, question) -%}
 {% if answer.endorsed %}accepted-answer{% endif %} {% if answer.author_id==question.author_id %} answered-by-owner{% endif %} {% if answer.deleted %}deleted{% endif -%}
{%- endmacro -%}

{%- macro user_long_score_and_badge_summary(user) -%}
  {%- include "widgets/user_long_score_and_badge_summary.html" -%}
{%- endmacro -%}

{%- macro user_country_flag(user) -%}
    {% if user.country and user.country.code != 'none' and user.show_country %}
        <img class="flag" 
            {# src="{{ user.country.flag }}" this is better but not compatible with v 1.0.5 #}
            src="{{ ('/images/flags/' ~ user.country.code|lower ~ '.gif')|media }}" {# use 'local' copy of flags #}
            alt="{% trans 
                    country=user.country.name 
                    %}flag of {{country}}{% 
                endtrans %}"
            title="{% trans 
                    country=user.country.name, 
                    person=user.username|escape %}{{person}} is from {{country}}{%
                 endtrans %}"
        />
    {% endif %}
{%- endmacro -%}

{%- macro user_country_name_and_flag(user) -%}
    {% if user.country and user.show_country %}
        {{ user.country.name }}
        {{ user_country_flag(user) }}
    {% endif %}
{%- endmacro -%}

{%- macro user_full_location(user) -%}
    {% if user.location %}
        {{ user.location|escape }},
    {% endif %}
    {{ user_country_name_and_flag(user) }}
{%- endmacro -%}

{% macro user_card(user) %}
    {% include "widgets/user_card.html" %}
{% endmacro %}

{%- macro user_list(users) -%}
    {% include "widgets/user_list.html" %}
{%- endmacro -%}

{#todo: rename this to avatar #}
{%- macro gravatar(user, size) -%}
{% spaceless %}
<a class="js-avatar-box"
    href="{{ user.get_absolute_url() }}"
><img class="js-avatar gravatar" 
    width="{{size}}" height="{{size}}"
    src="{{ user.get_avatar_url(size) }}"
    title="{{user.username|escape}}" 
    alt="{% trans username=user.username|escape %}{{username}} gravatar image{% endtrans %}" 
/></a>
{% endspaceless %}
{%- endmacro -%}

{%- macro user_website_link(user, max_display_length=25, force_show=False) -%}
    {% if user.website and (force_show or (not (user.is_blocked() or user.is_watched()))) %}
        <a 
            href="{{user.website|escape}}"
            title="{% trans username=user.username|escape, url=user.website|escape %}{{username}}'s website is {{url}}{% endtrans %}"
            {% if user.can_have_strong_url() == False %}
            rel="nofollow"
            {% endif %}
        >
        {{user.website|truncate(length=max_display_length, killwords=True, end='...')}}
        </a>
    {% endif %}
{%- endmacro -%}

{%- macro paginator(p, position='left', anchor='') -%}{# p is paginator context dictionary #}
{% spaceless %}
    {% if p.is_paginated %}
        <div 
            class="paginator"
            style="float:{{ position }}"
            data-num-pages="{{ p.pages }}"
        >
        <span class="prev"
            {% if not p.has_previous %}style="display: none"{% endif %}
        ><a href="{{p.base_url}}page={{ p.previous }}{{ anchor }}" 
            title="{% trans %}previous{% endtrans %}"
        >&laquo; {% trans %}previous{% endtrans %}</a></span>

        <span class="first-page-nav"
            {% if p.in_leading_range %}style="display: none"{% endif %}
        >
            <span class="page first-page"
                data-page="1"
            >
                <a href="{{p.base_url}}page={{1}}{{ anchor }}" >1</a>
            </span>
            <span class="leading-sep">...</span>
        </span>
         
        <span class="main-pages-nav">
        {% for num in p.page_numbers %}
            {% if num == p.page %}
                <span class="curr page" 
                    data-page="{{ num }}"
                    title="{% trans %}current page{% endtrans %}"
                ><a>{{ num }}</a></span>
            {% else %}
                <span class="page"
                    data-page="{{ num }}"
                ><a href="{{p.base_url}}page={{ num }}{{ anchor }}"
                    title="{% trans %}page {{num}}{% endtrans %}"
                >{{ num }}</a></span>
            {% endif %}
        {% endfor %}
        </span>

        {# the "last page" navigation #}
        <span class="last-page-nav"
            {% if p.in_trailing_range %}style="display: none"{% endif %}
        >
            <span class="trailing-sep">...</span>
            <span class="page last-page" data-page="{{ p.pages }}">
                <a href="{{ p.base_url }}page={{ p.pages }}{{ anchor }}"
                    title="{% trans num=p.pages %}page {{ num }}{% endtrans %}"
                >{{ p.pages }}</a>
            </span>
        </span>

        {# "next" nav link #}
        <span class="next"
            {% if not p.has_next %}style="display: none"{% endif %}
        ><a href="{{p.base_url}}page={{ p.next }}{{ anchor }}"
                title="{% trans %}next page{% endtrans %}"
        >{% trans %}next{% endtrans %} &raquo;</a>
        </span>

    </div>
    {% endif %}
{% endspaceless %}
{%- endmacro -%}


{%- macro paginator_main_page(p, position, search_state) -%} {# p is paginator context dictionary #}
    {% spaceless %}
        {% if p.is_paginated %}
            <div class="paginator" style="float:{{position}}">
                {% if p.has_previous %}
                    <span class="prev"><a href="{{ search_state.change_page(p.previous).full_url() }}" title="{% trans %}previous{% endtrans %}">
                        &laquo; {% trans %}previous{% endtrans %}</a></span>
                {% endif %}
                {% if not p.in_leading_range %}
                    {% for num in p.pages_outside_trailing_range %}
                        <span class="page"><a href="{{ search_state.change_page(num).full_url() }}" >{{ num }}</a></span>
                    {% endfor %}
                    ...
                {% endif %}

                {% for num in p.page_numbers %}
                    {% if num == p.page and p.pages != 1%}
                        <span class="curr page" title="{% trans %}current page{% endtrans %}"><a>{{ num }}</a></span>
                    {% else %}
                        <span class="page"><a href="{{ search_state.change_page(num).full_url() }}" title="{% trans %}page {{num}}{% endtrans %}">{{ num }}</a></span>
                    {% endif %}
                {% endfor %}

                {% if not p.in_trailing_range %}
                    ...
                    {% for num in p.pages_outside_leading_range|reverse %}
                        <span class="page"><a href="{{ search_state.change_page(num).full_url() }}" title="{% trans %}page {{ num }}{% endtrans %}">{{ num }}</a></span>
                    {% endfor %}
                {% endif %}
                {% if p.has_next %}
                    <span class="next"><a href="{{ search_state.change_page(p.next).full_url() }}" title="{% trans %}next page{% endtrans %}">{% trans %}next page{% endtrans %} &raquo;</a></span>
                {% endif %}
            </div>
        {% endif %}
    {% endspaceless %}
{%- endmacro -%}


{%- macro inbox_link(user) -%}
    {% if user.new_response_count %}
    <a id='ab-responses' href="{{user.get_absolute_url()}}?sort=inbox&section=forum">
        <img 
            alt="{% trans username=user.username|escape %}responses for {{username}}{% endtrans %}"
            src="{{ "/images/mail-envelope-full.png"|media }}"
            title="{% trans response_count=user.new_response_count %}you have {{response_count}} new response{% pluralize %}you have {{response_count}} new responses{% endtrans %}"
        />
    </a>
    {% endif %}
{%- endmacro -%}

{%- macro moderation_items_link(user, moderation_items) -%}
    {% if moderation_items %}
        <a id="ab-responses" href="{% url moderation_queue %}">
        {% if moderation_items['new_count'] > 0 %}
            <img src="{{'/images/dialog-warning.png'|media}}"
            {% if moderation_items['seen_count'] > 0 %}
                alt="{% trans new=moderation_items['new_count'], seen=moderation_items['seen_count']%}{{new}} new flagged posts and {{seen}} previous{% endtrans %}"
                title="{% trans new=moderation_items['new_count'], seen=moderation_items['seen_count']%}{{new}} new flagged posts and {{seen}} previous{% endtrans %}"
            {% else %}
                alt="{% trans new=moderation_items['new_count'] %}{{new}} new flagged posts{% endtrans %}"
                title="{% trans new=moderation_items['new_count'] %}{{new}} new flagged posts{% endtrans %}"
            {% endif %}
            />
        {% elif moderation_items['seen_count'] > 0 %}
            <img src={{'/images/dialog-warning-off.png'|media}} 
                alt="{% trans seen=moderation_items['seen_count'] %}{{seen}} flagged posts{% endtrans %}" 
                title="{% trans seen=moderation_items['seen_count'] %}{{seen}} flagged posts{% endtrans %}" 
            />
        {% endif %}
        </a>
    {% endif %}
{%- endmacro -%}

{%- macro timeago(datetime_object, action_performed=None) -%}
    {% set when='<abbr class="timeago" title="' 
                ~ datetime_object.replace(microsecond=0)|add_tz_offset
                ~ '">'
                ~ datetime_object.replace(microsecond=0)|add_tz_offset
                ~ '</abbr>' 
    %}
    {% if action_performed %}
        {# action and time can be reversed, depending on language #}
        {% trans %}{{ action_performed }} {{ when }}{% endtrans %}
    {% else %}
        {{ when }}
    {% endif %}
{%- endmacro -%}

{% macro one_shot_form_js(form=None, submit_button=None) %}
    <script type="text/javascript">
        (function() {
            var form = new OneShotForm();
            form.setSubmitButton($("{{submit_button}}"));
            form.decorate($("{{form}}"));
        })();
    </script>
{% endmacro %}

{% macro form_field_with_errors(field) %}
    {% if field.errors %} 
        <p class="error">{{ field.errors|join(", ") }}</p>
    {% endif %}
    {{ field }}
{% endmacro %}
